package com.tian.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tian.mapper.ItemDescMapper;
import com.tian.mapper.ItemMapper;
import com.tian.pojo.Item;
import com.tian.pojo.ItemDesc;
import com.tian.pojo.User;
import com.tian.vo.ItemVO;
import com.tian.vo.PageResult;
import com.tian.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

@Service
public class ItemServiceImpl implements ItemService{
    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private ItemDescMapper itemDescMapper;

    @Override
    public PageResult findItemByPage(PageResult pageResult) {
        IPage<Item> page =
                new Page<>(pageResult.getPageNum(),pageResult.getPageSize());
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        boolean b = StringUtils.hasLength(pageResult.getQuery());
        queryWrapper.like(b,"title",pageResult.getQuery());
        //经过程序分页,其中的数据全部获取
        page =itemMapper.selectPage(page,queryWrapper);
        long total = page.getTotal();
        List<Item> itemList = page.getRecords();
        return pageResult.setTotal(total).setRows(itemList);
    }

    @Override
    public void updateStatus(Item item) {

        itemMapper.updateById(item);
    }


    /**
     * 完成2张彪的入库操作
     * 1.item表
     * 2.item_Desc表 要求id统一
     * @param itemVO
     */
    @Override
    @Transactional  //控制事务
    public void saveItem(ItemVO itemVO) {
        //1.获取商品对象
        Item item=itemVO.getItem();
        item.setStatus(true);
        //MP已经实现了主键自动回显 ID有值
        itemMapper.insert(item);
        //2.获取商品信息
        ItemDesc itemDesc = itemVO.getItemDesc();
        itemDesc.setId(item.getId());
        itemDescMapper.insert(itemDesc);

    }

    @Override
    public void deleteNoticeById(Integer id) {
        itemMapper.deleteById(id);
    }

    @Override
    public void updateItem(Item item) {
        itemMapper.updateById(item);
    }
    @Override
    public Item getById(Integer id) {

        return itemMapper.selectById(id);
    }

}



